package com.gaogis.platform.filter;


import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import javax.servlet.Filter;
import java.util.Arrays;
import java.util.Collection;
import java.util.ArrayList;

//@Configuration
public class CorsConfig{

    @Bean
    public FilterRegistrationBean<Filter> registerCors(){
        UrlBasedCorsConfigurationSource source= new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**",buildConfig());
        Filter f=new CorsFilter(source);
        FilterRegistrationBean<Filter> registration= new FilterRegistrationBean<>(f);
        Collection<String> urls=new ArrayList<>();
        urls.add("/*");
        registration.setUrlPatterns(urls);
        registration.setOrder(0);
        return registration;
    }

    public CorsConfiguration buildConfig(){
        CorsConfiguration corsConfig = new CorsConfiguration();
        corsConfig.addAllowedOriginPattern("*");
        corsConfig.addAllowedHeader("*");
        corsConfig.addAllowedMethod("*");
        return corsConfig;

    }

}
